package com.ruoyi.core.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @Description: 线程池配置
 * @Company: 北京卡拉卡尔股份技术有限公司
 * @Author: Darren Ho
 * @Date: Created on 2019/4/24
 * @Version: V1.0
 */
@Configuration
public class ThreadPoolManagerConfig {

    @Value("${concurrency.core.pool.size:10}")
    private int concurrencyCorePoolSize;

    @Value("${concurrency.queue.capacity:10000}")
    private int concurrencyQueueCapacity = 10000;

    /**
     * 并发任务调度线程池
     */
    @Bean
    public ExecutorService concurrencyExecutorThreadPool() {

        /**
         * 线程数和并发量设置一致，最佳为2
         */
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(concurrencyCorePoolSize, concurrencyCorePoolSize, 60L, TimeUnit.SECONDS,
                                                                       new LinkedBlockingQueue<Runnable>(concurrencyQueueCapacity), threadFactory);

        return threadPoolExecutor;
    }


}
